package lgq.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import util.Page;
import vo.TrainVo;

import dao.EmpDAO;
import dao.TrainDAO;
import dao.impl.EmpDAOImpl;
import dao.impl.TrainDAOImpl;

import model.Train;
import model.TrainCourse;
import model.TrainProperty;
import model.TrainType;
import model.TrainWay;

public class TrainManager {
	public boolean addTrain(Train train)
	{
		boolean flag=false;
		TrainDAO td=new TrainDAOImpl();
		flag=td.addTrain(train);
		//����ѵ�γ̽��д���		
		TrainCourseManager tcm=new TrainCourseManager();
		flag=tcm.addTrainCourse(train.getTrainCourse());

		return flag;
	}
	public boolean ifEmpidExist(int id)
	{
		EmpDAO ed=new EmpDAOImpl();
		if(null==ed.getEmpById(id).getName())
		{
			return false;
		}else
		{
			return true;
			
		}
	}
	public Page<Train> getAll(int pageNo, int pageSize)
	{
		TrainDAO td=new TrainDAOImpl();
		return td.getAll(pageNo, pageSize);
	}
	public boolean ifEmpNoExist(String empno)
	{
		return ifEmpidExist(this.getIDByEmpno(empno));
	}
	public int getIDByEmpno(String empno)
	{
		
		return new EmpDAOImpl().getIDByEmpno(empno);
	}
	public TrainVo getById(int id)
	{
		TrainDAO td=new TrainDAOImpl();
		Train t=new Train();
		t=td.getById(id);
		TrainVo tv=new TrainVo();
		tv.setId(t.getId());
		tv.setAddress(t.getAddress());
		tv.setManager(t.getManager());
		tv.setTrainCourse(t.getTrainCourse());
		tv.setMoney(t.getMoney());
		tv.setTrainname(t.getTrainname());
		tv.setTrainProperty(t.getTrainProperty());
		tv.setTrainType(t.getTrainType());
		tv.setTrainWay(t.getTrainWay());
		tv.setOrganization(t.getOrganization());
		tv.setStarttime(new SimpleDateFormat("yyyy-MM-dd").format(t.getStarttime()));
		tv.setEndtime(new SimpleDateFormat("yyyy-MM-dd").format(t.getEndtime()));
		return tv;
	}
	
	public boolean updateTrain(Train t)
	{
		TrainDAO td=new TrainDAOImpl();
		return td.updateTrain(t);
	}
	
	public Page<Train> getByName(int pageNo,int pageSize,String name)
	{
		TrainDAO td=new TrainDAOImpl();
		return td.getByTrainName(pageNo, pageSize, name);
	}
	public Page<Train> getByProperty(int pageNo, int pageSize,TrainProperty trainProperty)
	{
		TrainDAO td=new TrainDAOImpl();
		return td.getByProperty(pageNo, pageSize, trainProperty);
	}
	
	public Page<Train> getByTrainType(int pageNo, int pageSize,TrainType trainType) 
	{
		TrainDAO td=new TrainDAOImpl();
		return td.getByTrainType(pageNo, pageSize, trainType);
	}
	
	public Page<Train> getByTrainWay(int pageNo, int pageSize, TrainWay trainWay) 
	{
		TrainDAO td=new TrainDAOImpl();
		return td.getByTrainWay(pageNo, pageSize, trainWay);
	}
	public Page<Train> getByTime(int pageNo, int pageSize, Date starttime,
			Date endtime) {
		TrainDAO td=new TrainDAOImpl();
		return td.getByTime(pageNo, pageSize, starttime, endtime);
	}
	public boolean deleteById(int id)
	{
		TrainDAO td=new TrainDAOImpl();
		return td.deleteById(id);
	}
}
